package fr.obsmip.sedoo.client.ui.menu;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.obsmip.sedoo.client.GlobalBundle;
import fr.obsmip.sedoo.client.message.Message;
import fr.obsmip.sedoo.client.place.GeoSummaryPlace;
import fr.obsmip.sedoo.client.place.MetadataResultPlace;
import fr.obsmip.sedoo.client.place.MetadataSearchPlace;
import fr.obsmip.sedoo.client.ui.misc.MenuLink;
import fr.obsmip.sedoo.shared.domain.SearchCriteriaDTO;
import fr.sedoo.commons.client.message.CommonMessages;
import fr.sedoo.commons.client.util.StringUtil;
import fr.sedoo.commons.client.widget.DialogBoxTools;

public class MetadataUserMenu extends AbstractMenu implements ClickHandler {
	
	private MenuLink observationAreaLink;
	private MenuLink advancedSearchLink;
	private MenuLink geologyBrowseLink;
	private MenuLink climateBrowseLink;
	private Image searchButton;
	private TextBox keyword;
	
public MetadataUserMenu() {
	
		VerticalPanel panel = getMetadataPanel(); 
	    initWidget(panel);
	}

private VerticalPanel getMetadataPanel() {
	VerticalPanel metadataPanel = new VerticalPanel();
	metadataPanel.getElement().getStyle().setPaddingLeft(3, Unit.PX);
	advancedSearchLink = new MenuLink(Message.INSTANCE.metadataUserMenuAdvancedSearchLink());
	
	advancedSearchLink.setTitle(Message.INSTANCE.metadataUserMenuAdvancedSearchLink());
	advancedSearchLink.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) 
	        {
	        	getPresenter().goTo(new MetadataSearchPlace());
	        }
	});
	
	observationAreaLink = new MenuLink(Message.INSTANCE.metadataUserMenuCatalogByObservatoryLink());
	observationAreaLink.addStyleName("leftPadding5px");
	observationAreaLink.setTitle(Message.INSTANCE.metadataUserMenuCatalogByObservatoryLink());
	observationAreaLink.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) 
	        {
	        	getPresenter().goTo(new GeoSummaryPlace());
	        }
	});
	
	
	geologyBrowseLink = new MenuLink(Message.INSTANCE.metadataUserMenuCatalogByGeologyLink());
	geologyBrowseLink.setTitle(Message.INSTANCE.metadataUserMenuCatalogByGeologyLink());
	geologyBrowseLink.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) 
	        {
	        	DialogBoxTools.modalAlert("Information", "Not yet implemented");
	        	//getPresenter().goTo(new OneLevelThesaurusPlace(OneLevelThesaurusPlace.GEOLOGY_THESAURUS));
	        }
	});
	
	climateBrowseLink = new MenuLink(Message.INSTANCE.metadataUserMenuCatalogByClimateLink());
	climateBrowseLink.setTitle(Message.INSTANCE.metadataUserMenuCatalogByClimateLink());
	climateBrowseLink.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) 
	        {
	        	DialogBoxTools.modalAlert("Information", "Not yet implemented");
	        	//getPresenter().goTo(new OneLevelThesaurusPlace(OneLevelThesaurusPlace.CLIMATE_THESAURUS));
	        }
	});
	
	Label catalogBrowseSubmenuTitle = new Label(Message.INSTANCE.metadataUserMenuCatalogBrowseLink());
	catalogBrowseSubmenuTitle.setStyleName("inactiveMenuLink");
	catalogBrowseSubmenuTitle.setHeight("16px");
	
	metadataPanel.add(catalogBrowseSubmenuTitle);
	
	
	VerticalPanel browsePanel = new VerticalPanel();
	browsePanel.setSpacing(5);
	
	browsePanel.add(observationAreaLink);
//	browsePanel.add(climateBrowseLink);
//	browsePanel.add(geologyBrowseLink);
	
	
	Label metadataSearchSubmenuTitle = new Label(Message.INSTANCE.search());
	metadataSearchSubmenuTitle.setStyleName("inactiveMenuLink");
	
	metadataPanel.add(browsePanel);
	
	
	VerticalPanel searchPanel = new VerticalPanel();
	searchPanel.setSpacing(5);
	searchPanel.add(advancedSearchLink);
	searchPanel.addStyleName("leftPadding5px");
	
//	secondaryPanel.add(metadataCreatingLink);
	
	HorizontalPanel quickSearchPanel = new HorizontalPanel();

	quickSearchPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	metadataPanel.add(metadataSearchSubmenuTitle);
	keyword = new TextBox();
	
	keyword.getElement().setAttribute("placeHolder", Message.INSTANCE.metadataUserMenuQuickSearchPlaceholder());
	quickSearchPanel.add(keyword);
	
	keyword.setTitle(Message.INSTANCE.metadataUserMenuKeywordTooltip());
	
	searchButton = new Image(GlobalBundle.INSTANCE.search());
	searchButton.addClickHandler(this);
	searchButton.getElement().getStyle().setMarginLeft(3, Unit.PX);
	searchButton.getElement().getStyle().setProperty("cursor", "pointer");
	searchButton.setTitle(Message.INSTANCE.metadataUserMenuQuickSearchTooltip());
	quickSearchPanel.add(searchButton);
	
	
	searchPanel.add(quickSearchPanel);
	
	
	
	searchPanel.add(advancedSearchLink);
	
	metadataPanel.add(searchPanel);
	
	return metadataPanel;
}

@Override
public void onClick(ClickEvent event) {
	if (event.getSource() == searchButton)
	{
		if (StringUtil.isEmpty(keyword.getText()))
		{
			DialogBoxTools.modalAlert(CommonMessages.INSTANCE.information(), Message.INSTANCE.metadataUserMenuQuickSearchErrorMessage());
			return;
		}

		String keywords = keyword.getText().trim();
		List<String> keywordList = new ArrayList<String>();
			String[] aux = keywords.split(",");
			for (int i = 0; i < aux.length; i++) 
			{
				keywordList.add(aux[i]);
			}
		
		MetadataResultPlace metadataResultPlace = new MetadataResultPlace();
		SearchCriteriaDTO criteria = new SearchCriteriaDTO();
		criteria.setKeywordsLogicalLink(SearchCriteriaDTO.OR);
		criteria.setKeywords(keywordList);
		metadataResultPlace.setCriteria(criteria);
		getPresenter().goTo(metadataResultPlace);
	}
	
}




}
